package com.zghhost.codedemo.graphql;

import com.alibaba.fastjson.JSONObject;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.Scalars;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

import java.util.Map;

import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;

public class HelloWorld {

    public static void main(String[] args) {
        GraphQLObjectType beanVo = newObject().name("zy")
                .field(newFieldDefinition().name("id").type(Scalars.GraphQLInt).build())//类型一定要与结构类字段的类型相对应
                .field(newFieldDefinition().name("xy").type(Scalars.GraphQLInt).build())
                .field(newFieldDefinition().name("code").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("title").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("jc").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("pic").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("general").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("text").type(Scalars.GraphQLString).build())
                .field(newFieldDefinition().name("readnum").type(Scalars.GraphQLInt).build())
                .build();

        GraphQLFieldDefinition findforid = GraphQLFieldDefinition.newFieldDefinition().name("zy")
                .argument(//这里用GraphQL些查询语句时传入的参数
                        newArgument()
                                .name("id")//参数名为id
                                .type(Scalars.GraphQLInt)//参数类型
                                .build()
                )
                .type(beanVo)//绑定GraphQL的一个结构，就是上面的那段代码
                .dataFetcher(environment -> {
                    // 获取查询参数
                    int id = environment.getArgument("id");
                    ZyVo vo = null;
                    try {
                        vo = new ZyVo();
                        vo.setCode("123");
                        vo.setGeneral("123");
                        vo.setId(1);
                        vo.setJc("123");
                        vo.setPic("123");
                        vo.setReadnum(123);
                        vo.setText("123");
                        vo.setTitle("123");
                        vo.setXy(123);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                    System.out.println(vo);

                    return vo;
                }).build();
        GraphQLSchema schema = GraphQLSchema.newSchema()
                .query(newObject().name("GraphQuery")
                        .field(findforid)//半桶水不知道怎么描述它
                        .build()
                )
                .build();

        String ghql = "{zy(id:1){id,title,pic,jc}}";
//最后就是获取结果了
        ExecutionResult re = new GraphQL(schema).execute(ghql);
        Map<String, Object> result1 = (Map<String, Object>) re.getData();
//把它打印出来
        System.out.println(result1);//结果：{zy={id=1, title=服装与服饰设计, readnum=0, text=null, pic=null, jc=null}}
//可以把它加入到json对象中

        JSONObject json = new JSONObject();
        json.putAll(result1);
        System.out.println(json);//输出就成了json数据了：{"zy":{"id":1,"title":"服装与服饰设计","readnum":0}}
    }
}